﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using UniversityManagementSystem.DAL;

namespace UniversityManagementSystem.UI
{
    public partial class StudentResultEntry : System.Web.UI.Page
    {
        Student aStudent = new Student();
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (!IsPostBack)
                {
                    Department department = new Department();
                    List<Department> allDepartments = new List<Department>();
                    allDepartments = department.GetAllDepartments();
                    departmentDropDownList.DataSource = allDepartments;
                    departmentDropDownList.DataValueField = "DepartmentName";
                    departmentDropDownList.DataBind();
                    departmentDropDownList_SelectedIndexChanged(null,null);
                }
            }
            catch (Exception)
            {
                messageLabel.Text = "Error occured while loading the page.";
                messageLabel.ForeColor = System.Drawing.Color.Red;
            }
        }

        protected void departmentDropDownList_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                List<Course> allCourses = new List<Course>();
                Course course = new Course();
                allCourses = course.GetAllCoursesByDepartment(departmentDropDownList.SelectedValue);
                courseDropDownList.DataSource = allCourses;
                courseDropDownList.DataValueField = "CourseTitle";
                courseDropDownList.DataTextField = "CourseName";
                courseDropDownList.DataBind();
            }
            catch (Exception exception)
            {
                messageLabel.Text = "Unable to load courses.";
                messageLabel.ForeColor = System.Drawing.Color.Red;
            }
        }

        protected void searchButton_Click(object sender, EventArgs e)
        {
            try
            {
                aStudent = aStudent.SearchAstudent(studentRegTextBox.Text);
                ViewState["aStudent"] = aStudent;
                studentNameTextBox.Text = aStudent.name;
                studentEmailTextBox.Text = aStudent.email;
            }
            catch (Exception exception)
            {
                messageLabel.Text = "Unable to find student,try again later.";
                messageLabel.ForeColor = System.Drawing.Color.Red;
            }
            
        }

        protected void saveButton_Click(object sender, EventArgs e)
        {
            string saveResult = "";
            bool resultAdded = false;
            try
            {
                Student aStudent = new Student();
                aStudent = (Student) ViewState["aStudent"];
                Course course = new Course();
                if(!course.CheckCourse(aStudent.RegistrationNo,courseDropDownList.SelectedValue))
                {
                    saveResult = courseDropDownList.SelectedValue + "is not enrolled to this student";
                    messageLabel.ForeColor = System.Drawing.Color.Red;
                }
                else
                {
                    saveResult = course.SaveResult(aStudent.RegistrationNo,courseDropDownList.SelectedValue,obtainedGradeDropDownList.SelectedValue);
                    messageLabel.ForeColor = System.Drawing.Color.Blue;
                }
                messageLabel.Text = saveResult;
            }
            catch (Exception exception)
            {
                messageLabel.Text = "Unable to save result.";
                messageLabel.ForeColor = System.Drawing.Color.Red;
            }
        }
    }
}